/*
 * ProductBean.java
 *
 * Created on 13 novembre 2007, 15:51
 */
package com.reimscopie.beans.entities;

import java.util.* ;

import javax.persistence.* ;

import com.reimscopie.* ;


/**
 * This abstract entity bean represent a product and will be extended by other
 * beans.
 */
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@NamedQuery(name="search-product",
           query="SELECT id FROM ProductBean o "+
                 "WHERE (o.name LIKE ?1 OR o.description LIKE ?1) AND o.valid=true")
public abstract class ProductBean<T extends Product> extends ValueBean<T> {
  
  // name of the product
  private String name ;
  
  // a description of the product
  private String description;
  
  // the list of comment for this product
  private Collection<CommentBean> comments ;
  
  // product valid or not
  private boolean valid ;
  
  // a picture of the product
  private String picture ;
  
  
  /**
   * Create a new instance of <code>ProductBean</code> (default constructor).
   */
  public ProductBean() {
  }
  
  
  /**
   * Create a new instance of <code>ProductBean</code> with all attributes.
   *
   * @param product The valued product.
   */
  public ProductBean(Product product) {
    
    if (product.hasId())
      throw new IllegalArgumentException("product has an image") ;
    
    this.name        = product.getName() ;
    this.description = product.getDescription() ;
    this.valid       = product.isValid() ;
    this.picture     = product.getPicture() ;
    
    comments = new ArrayList<CommentBean>() ;
    
  }
  
  
  /**
   * Return the name of this product.
   *
   * @return The name of this product.
   */
  public String getName() {
    return name ;
  }
  
  
  /**
   * Set the name of this product.
   *
   * @param name The new name of this product.
   */
  public void setName(String name) {
    this.name = name ;
  }
  
  
  /**
   * Return a short description of this product.
   *
   * @return The description of this product.
   */
  public String getDescription() {
    return description ;
  }
  
  
  /**
   * Set a short description of this product.
   *
   * @param description The new description of this product.
   */
  public void setDescription(String description) {
    this.description = description ;
  }
  
  
  /**
   * Return the comment list added for this product.
   *
   * @return All comments added for this product.
   */
  @OneToMany
  public Collection<CommentBean> getComments() {
    return comments ;
  }
  
  
  /**
   * Set the collection of comments added for this product.
   *
   * @param comments The list of comments.
   */
  public void setComments(Collection<CommentBean> comments) {
    this.comments = comments ;
  }
  
  
  /**
   * Add a comments for this product.
   *
   * @param comment The new comment.
   */
  public void addComment(CommentBean comment) {
    comments.add(comment) ;
  }
  
  
  /**
   * Returns if the product is valid.
   *
   * @return <code>true</code> if the product is valid, <code>false</code> if
   * it is obsolete.
   */
  public boolean getValid() {
    return valid ;
  }
  
  
  /**
   * Set if the product is valid.
   *
   * @param valid If the product is valid or obselete.
   */
  public void setValid(boolean valid) {
    this.valid = valid ;
  }
  
  
  /**
   * Returns the product's picture.
   *
   * @return the picture of this product.
   */
  public String getPicture() {
    return picture ;
  }
  
  
  /**
   * Set if the product is valid.
   *
   * @param picture The new picture name.
   */
  public void setPicture(String picture) {
    this.picture = picture ;
  }
  
  
////////////////////////////////////////////////////////////////////////////////
//                           TRANSFOMATION                                    //
////////////////////////////////////////////////////////////////////////////////

  /**
   * Transforms this entity bean into the associated product.
   *
   * @return A new product instance.
   *
   * @see #toValue
   */
  public T toProduct() {
    return toValue() ;
  }
  
}
